package com.geekhale.validate.controller;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RequestMapping("validate")
@RestController
@Slf4j
public class ValidateController {

    @Autowired
    private UserApplication userApplication;


    @GetMapping("createUser1")
    public String createUser(@Valid User user){
        log.info("user {}", user);
        return "SUCCESS";
    }

    @GetMapping("createUser2")
    public String createUser2(CreateUser user){
        log.info("user {}", user);
        CreateUserCommand createUserCommand = new CreateUserCommand();
        createUserCommand.setUser(user.user);
        this.userApplication.createUser(createUserCommand);
        return "SUCCESS";
    }

    @Data
    static class CreateUser{
        @ValidByService
        private User user;
    }
}
